package com.ivideo.backend.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ivideo.backend.base.utils.Result;
import com.ivideo.backend.base.utils.ResultCodeEnum;
import com.ivideo.backend.model.system.SysUser;
import com.ivideo.backend.model.vo.LoginVo;
import com.ivideo.backend.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Project: ivideo-parent
 * @Name: SysUserController
 * @Author: Jack_Li
 * @Date: 2024/5/20 15:45
 */

@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/system/user")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @ApiOperation(value = "单点登录")
    @GetMapping(value = "/info")
    public Result info()
    {
        Map<String,Object> map = new HashMap<>();
        map.put("roles","[admin]");
        map.put("introduction","I am a super administrator");
        map.put("name","Super Admin");
        map.put("avatar","https://img03.sogoucdn.com/app/a/100520093/d71a6360ba8601ff-19264876bfd6a308-dfbd047d3bb4f2621f01d7ae18979b6e.jpg");
        return Result.success(map);
    }

    /**
     * 用户登录
     * @param systemUserVo
     * @return
     */
    @ApiOperation(value = "用户登录")
    @PostMapping(value = "/login")
    public Result login(@RequestBody LoginVo systemUserVo) {
        log.info("login User：{}", systemUserVo);
        if(systemUserVo == null || systemUserVo.getUsername() == null || systemUserVo.getPassword() == null) {
            return Result.error(ResultCodeEnum.NULL.getCode(), "用户名或密码不能为空");
        }
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, systemUserVo.getUsername());
        queryWrapper.eq(SysUser::getPassword, systemUserVo.getPassword());
        SysUser user = sysUserService.getOne(queryWrapper);
        if (user == null) {
            return Result.error(ResultCodeEnum.USER_OR_PASSWORD_ERROR.getCode(), "用户名或密码错误");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("token","admin-token");
        return Result.success(map);
    }

    /**
     * 用户注册
     * @param sysUser
     * @return
     */
    @ApiOperation(value = "用户注册")
    @PostMapping(value = "/register")
    public Result register(@RequestBody SysUser sysUser) {
        log.info("register User：{}", sysUser);
        if(sysUser == null || sysUser.getUsername() == null || sysUser.getPassword() == null) {
            return Result.error(ResultCodeEnum.NULL.getCode(), "用户名或密码不能为空");
        }
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, sysUser.getUsername());
        SysUser user = sysUserService.getOne(queryWrapper);
        if (user != null) {
            return Result.error(ResultCodeEnum.USER_OR_PASSWORD_ERROR.getCode(), "用户名已存在");
        }
        sysUserService.save(sysUser);
        return Result.success(null);
    }

    /**
     * 用户列表
     * @param page
     * @param size
     * @param keyword
     * @return
     */
    @ApiOperation(value = "用户列表")
    @GetMapping(value = "/list/{page}/{size}")
    public Result<Page<SysUser>> list(
            @PathVariable Long page,
            @PathVariable Long size,
            @RequestParam(required = false) String keyword) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        if (keyword != null) {
            queryWrapper.like(SysUser::getName, keyword);
        }
        Page<SysUser> userPage = new Page<SysUser>(page, size);
        sysUserService.page(userPage, queryWrapper);
        log.info("userList：{}", userPage.getRecords());
        return Result.success(userPage);
    }

    /**
     * 添加或修改用户
     * @param sysUser
     * @return
     */
    @ApiOperation(value = "添加或修改用户")
    @PostMapping(value = "/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SysUser sysUser) {
        log.info("saveOrUpdate User：{}", sysUser);
        if(sysUser == null || sysUser.getUsername() == null) {
            return Result.error(ResultCodeEnum.NULL.getCode(), "用户名不能为空");
        }
        if (sysUser.getId() == null) {
            LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysUser::getUsername, sysUser.getUsername());
            SysUser user = sysUserService.getOne(queryWrapper);
            if (user != null) {
                return Result.error(ResultCodeEnum.USER_OR_PASSWORD_ERROR.getCode(), "用户名已存在");
            }
        }
        sysUserService.saveOrUpdate(sysUser);
        return Result.success(null);
    }

    /**
     * 用户删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "用户删除")
    @DeleteMapping(value = "/remove")
    public Result remove(@RequestBody List<Long> ids) {
        log.info("remove User：{}", ids);
        sysUserService.removeByIds(ids);
        return Result.success(null);
    }

    /**
     * 修改用户状态
     * @param id
     * @param status
     * @return
     */
    @ApiOperation(value = "修改用户状态")
    @PutMapping(value = "/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setStatus(status);
        log.info("updateStatus：{}", sysUser);
        sysUserService.updateById(sysUser);
        return Result.success(null);
    }

    /**
     * 用户退出
     * @return
     */
    @ApiOperation(value = "用户退出")
    @GetMapping(value = "/logout")
    public Result logout() {
        return Result.success(null);
    }

    /**
     * 测试
     * @return
     */
    @ApiOperation(value = "测试")
    @GetMapping(value = "/test")
    public Result test() {
        int i = 1 / 0;
        return Result.success(null);
    }

}
